﻿using BGYXControlLibrary.Enums;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Script.Serialization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Markup;
using System.Windows.Media;
using System.Windows.Resources;
using BGYX_Resources = BGYXControlLibrary.Properties.Resources;

namespace BGYXControlLibrary
{
    public class BGYX_Icon : Image
    {
        private static Dictionary<string, string> _iconsDict;
        static BGYX_Icon()
        {
            using (MemoryStream iconsMs = new MemoryStream(BGYX_Resources.icons_json))
            using (MemoryStream writeMs = new MemoryStream())
            using (var decompressor = new GZipStream(iconsMs, CompressionMode.Decompress))
            {
                decompressor.CopyTo(writeMs);
                writeMs.Position = 0;
                using (StreamReader sr = new StreamReader(writeMs, Encoding.UTF8))
                {
                    var iconsJsonStr = sr.ReadToEnd();

                    JavaScriptSerializer serializer = new JavaScriptSerializer();
                    _iconsDict = serializer.Deserialize<Dictionary<string, string>>(iconsJsonStr);
                }
            }   
        }
        public Icons Icon
        {
            get { return (Icons)GetValue(IconProperty); }
            set { SetValue(IconProperty, value); }
        }

        public static readonly DependencyProperty IconProperty = DependencyProperty.Register(
          "Icon",
          typeof(Icons),
          typeof(BGYX_Icon),
          new FrameworkPropertyMetadata(Icons.None, FrameworkPropertyMetadataOptions.AffectsArrange, new PropertyChangedCallback((d, e) => ((BGYX_Icon)d).ReferenControls()))
        );
        public Brush Color
        {
            get { return (Brush)GetValue(ColorProperty); }
            set { SetValue(ColorProperty, value); }
        }

        public static readonly DependencyProperty ColorProperty = DependencyProperty.Register(
          "Color",
          typeof(Brush),
          typeof(BGYX_Icon),
          new FrameworkPropertyMetadata(Brushes.Black, FrameworkPropertyMetadataOptions.AffectsArrange, new PropertyChangedCallback((d, e) => ((BGYX_Icon)d).ReferenControls()))
        );

        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();
            ReferenControls();
        }

        public BGYX_Icon()
        {

        }

        private static void IconChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
        {
            (d as BGYX_Icon).ReferenControls();
        }

        public void ReferenControls()
        {
            if (Icon == Icons.None)
            {
                this.Source = null;
                return;
            }
            var iconStr = Icon.ToString();

            DrawingImage drawImage = new DrawingImage();
            GeometryDrawing GeoDraw = new GeometryDrawing();
            GeoDraw.Brush = Color;

            drawImage.Drawing = GeoDraw;
            GeoDraw.Geometry = StreamGeometry.Parse(_iconsDict[iconStr]);
            this.Source = drawImage;
        }
    }
}
